home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / loaders.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-03-23  |  9KB  |  238 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Builtin template loaders that supply a Page or renderer with a
  5. document to render.
  6.  
  7. Nevow provides the following DocFactory loaders by default:
  8.  
  9.   - B{stan} - turn a stan tag tree into a DocFactory
  10.   - B{xmlfile} - load a well formed XML document from file
  11.   - B{htmlfile} - load a HTML file from disk
  12.   - B{xmlstr} - load a well formed XML document from a string
  13.   - B{htmlstr} - load a HTML document from a string
  14.  
  15. Unless absolutely necessary you should use either the stan loader or
  16. one of the xml loaders. The html loaders should only be used for badly
  17. formed HTML documents, i.e. if your HTML developer hasn't heard of
  18. XHTML yet. Even then, you should probably try to educate the HTML
  19. developer first ;-).
  20. """
  21. import warnings
  22. import os.path as os
  23. from zope.interface import implements
  24. from twisted.python.reflect import getClass
  25. from twisted.web import microdom
  26. from nevow import context
  27. from nevow import inevow
  28. from nevow import tags
  29. from nevow import flat
  30. from nevow.flat import flatsax
  31. from nevow.util import CachedFile
  32.  
  33. class stan(object):
  34.     '''A stan tags document factory'''
  35.     implements(inevow.IDocFactory)
  36.     stan = None
  37.     pattern = None
  38.     _cache = None
  39.     
  40.     def __init__(self, stan = None, pattern = None):
  41.         if stan is not None:
  42.             self.stan = stan
  43.         
  44.         if pattern is not None:
  45.             self.pattern = pattern
  46.         
  47.  
  48.     
  49.     def load(self, ctx = None, preprocessors = ()):
  50.         if self._cache is None:
  51.             stan = [
  52.                 self.stan]
  53.             for proc in preprocessors:
  54.                 stan = proc(stan)
  55.             
  56.             stan = flat.precompile(stan, ctx)
  57.             if self.pattern is not None:
  58.                 stan = inevow.IQ(stan).onePattern(self.pattern)
  59.             
  60.             self._cache = stan
  61.         
  62.         return self._cache
  63.  
  64.  
  65.  
  66. class htmlstr(object):
  67.     '''A document factory for HTML contained in a string'''
  68.     implements(inevow.IDocFactory)
  69.     template = None
  70.     pattern = None
  71.     beExtremelyLenient = True
  72.     _cache = None
  73.     
  74.     def __init__(self, template = None, pattern = None, beExtremelyLenient = None):
  75.         warnings.warn("[v0.8] htmlstr is deprecated because it's buggy. Please start using xmlfile and/or xmlstr.", DeprecationWarning, stacklevel = 2)
  76.         if template is not None:
  77.             self.template = template
  78.         
  79.         if pattern is not None:
  80.             self.pattern = pattern
  81.         
  82.         if beExtremelyLenient is not None:
  83.             self.beExtremelyLenient = beExtremelyLenient
  84.         
  85.  
  86.     
  87.     def load(self, ctx = None, preprocessors = ()):
  88.         if not not preprocessors:
  89.             raise AssertionError, 'preprocessors not supported by htmlstr'
  90.         return self._cache
  91.  
  92.  
  93.  
  94. class htmlfile(object):
  95.     '''A document factory for an HTML disk template'''
  96.     implements(inevow.IDocFactory)
  97.     template = None
  98.     pattern = None
  99.     templateDir = ''
  100.     beExtremelyLenient = True
  101.     
  102.     def __init__(self, template = None, pattern = None, templateDir = None, beExtremelyLenient = None):
  103.         warnings.warn("[v0.8] htmlfile is deprecated because it's buggy. Please start using xmlfile and/or xmlstr.", DeprecationWarning, stacklevel = 2)
  104.         if template is not None:
  105.             self.template = template
  106.         
  107.         if pattern is not None:
  108.             self.pattern = pattern
  109.         
  110.         if templateDir is not None:
  111.             self.templateDir = templateDir
  112.         
  113.         if beExtremelyLenient is not None:
  114.             self.beExtremelyLenient = beExtremelyLenient
  115.         
  116.         _filename = os.path.join(self.templateDir, self.template)
  117.         self._cache = CachedFile(_filename, self._reallyLoad)
  118.  
  119.     
  120.     def _reallyLoad(self, path, ctx):
  121.         doc = microdom.parse(path, beExtremelyLenient = self.beExtremelyLenient)
  122.         doc = flat.precompile(doc, ctx)
  123.         if self.pattern is not None:
  124.             doc = inevow.IQ(doc).onePattern(self.pattern)
  125.         
  126.         return doc
  127.  
  128.     
  129.     def load(self, ctx = None, preprocessors = ()):
  130.         if not not preprocessors:
  131.             raise AssertionError, 'preprocessors not supported by htmlfile'
  132.         return self._cache.load(ctx)
  133.  
  134.  
  135.  
  136. class xmlstr(object):
  137.     implements(inevow.IDocFactory)
  138.     template = None
  139.     pattern = None
  140.     ignoreDocType = None
  141.     ignoreComment = None
  142.     _cache = None
  143.     
  144.     def __init__(self, template = None, pattern = None, ignoreDocType = False, ignoreComment = False):
  145.         if template is not None:
  146.             self.template = template
  147.         
  148.         if pattern is not None:
  149.             self.pattern = pattern
  150.         
  151.         if ignoreDocType is not None:
  152.             self.ignoreDocType = ignoreDocType
  153.         
  154.         if ignoreComment is not None:
  155.             self.ignoreComment = ignoreComment
  156.         
  157.  
  158.     
  159.     def load(self, ctx = None, preprocessors = ()):
  160.         '''
  161.         Get an instance, possibly cached from a previous call, of this document
  162.         '''
  163.         if self._cache is None:
  164.             doc = flatsax.parseString(self.template, self.ignoreDocType, self.ignoreComment)
  165.             for proc in preprocessors:
  166.                 doc = proc(doc)
  167.             
  168.             doc = flat.precompile(doc, ctx)
  169.             if self.pattern is not None:
  170.                 doc = inevow.IQ(doc).onePattern(self.pattern)
  171.             
  172.             self._cache = doc
  173.         
  174.         return self._cache
  175.  
  176.  
  177.  
  178. class xmlfile(object):
  179.     implements(inevow.IDocFactory)
  180.     template = None
  181.     templateDir = None
  182.     pattern = None
  183.     ignoreDocType = False
  184.     ignoreComment = False
  185.     
  186.     def __init__(self, template = None, pattern = None, templateDir = None, ignoreDocType = None, ignoreComment = None):
  187.         self._cache = { }
  188.         if template is not None:
  189.             self.template = template
  190.         
  191.         if pattern is not None:
  192.             self.pattern = pattern
  193.         
  194.         if templateDir is not None:
  195.             self.templateDir = templateDir
  196.         
  197.         if ignoreDocType is not None:
  198.             self.ignoreDocType = ignoreDocType
  199.         
  200.         if ignoreComment is not None:
  201.             self.ignoreComment = ignoreComment
  202.         
  203.         if self.templateDir is not None:
  204.             self._filename = os.path.join(self.templateDir, self.template)
  205.         else:
  206.             self._filename = self.template
  207.         self._cache = { }
  208.  
  209.     
  210.     def load(self, ctx = None, preprocessors = ()):
  211.         rendererFactoryClass = None
  212.         if ctx is not None:
  213.             r = inevow.IRendererFactory(ctx, None)
  214.             if r is not None:
  215.                 rendererFactoryClass = getClass(r)
  216.             
  217.         
  218.         cacheKey = (self._filename, self.pattern, rendererFactoryClass)
  219.         cachedFile = self._cache.get(cacheKey)
  220.         if cachedFile is None:
  221.             cachedFile = self._cache[cacheKey] = CachedFile(self._filename, self._reallyLoad)
  222.         
  223.         return cachedFile.load(ctx, preprocessors)
  224.  
  225.     
  226.     def _reallyLoad(self, path, ctx, preprocessors):
  227.         doc = flatsax.parse(open(self._filename), self.ignoreDocType, self.ignoreComment)
  228.         for proc in preprocessors:
  229.             doc = proc(doc)
  230.         
  231.         doc = flat.precompile(doc, ctx)
  232.         if self.pattern is not None:
  233.             doc = inevow.IQ(doc).onePattern(self.pattern)
  234.         
  235.         return doc
  236.  
  237.  
  238.